package com.ruoyi.web.controller.qif;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.qif.domain.QifExpenditure;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qif.domain.QifRepair;
import com.ruoyi.qif.service.IQifRepairService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 锜风公寓-维修Controller
 *
 * @author David
 * @date 2025-11-03
 */
@Controller
@RequestMapping("/qif/repair")
public class QifRepairController extends BaseController {
    private String prefix = "qif/repair";

    @Autowired
    private IQifRepairService qifRepairService;

    @RequiresPermissions("qif:repair:view")
    @GetMapping()
    public String repair() {
        return prefix + "/repair";
    }

    @RequiresPermissions("qif:repair:import")
    @Log(title = "锜风公寓-维修导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        try {
            ExcelUtil<QifRepair> util = new ExcelUtil<QifRepair>(QifRepair.class);
            // 直接调用 importExcel 方法，指定工作表名称为'公账支出'
            List<QifRepair> qifExpenditureList = util.importExcel("日常维修", file.getInputStream(), 0);
            String message = qifRepairService.importQifExpenditure(qifExpenditureList, updateSupport);
            return AjaxResult.success(message);
        } catch (IOException e) {
            error("导入文件失败:" + e.getMessage());
            return AjaxResult.error("导入文件失败: " + e.getMessage());
        } catch (Exception e) {
            error("处理导入数据失败: " + e.getMessage());
            return AjaxResult.error("处理导入数据失败: " + e.getMessage());
        }
    }

    /**
     * 查询锜风公寓-维修列表
     */
    @RequiresPermissions("qif:repair:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QifRepair qifRepair) {
        startPage();
        List<QifRepair> list = qifRepairService.selectQifRepairList(qifRepair);
        return getDataTable(list);
    }

    /**
     * 导出锜风公寓-维修列表
     */
    @RequiresPermissions("qif:repair:export")
    @Log(title = "锜风公寓-维修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QifRepair qifRepair) {
        List<QifRepair> list = qifRepairService.selectQifRepairList(qifRepair);
        ExcelUtil<QifRepair> util = new ExcelUtil<QifRepair>(QifRepair.class);
        return util.exportExcel(list, "锜风公寓-维修数据");
    }

    /**
     * 新增锜风公寓-维修
     */
    @RequiresPermissions("qif:repair:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存锜风公寓-维修
     */
    @RequiresPermissions("qif:repair:add")
    @Log(title = "锜风公寓-维修", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QifRepair qifRepair) {
        return toAjax(qifRepairService.insertQifRepair(qifRepair));
    }

    /**
     * 修改锜风公寓-维修
     */
    @RequiresPermissions("qif:repair:edit")
    @GetMapping("/edit/{repairId}")
    public String edit(@PathVariable("repairId") Long repairId, ModelMap mmap) {
        QifRepair qifRepair = qifRepairService.selectQifRepairByRepairId(repairId);
        mmap.put("qifRepair", qifRepair);
        return prefix + "/edit";
    }

    /**
     * 修改保存锜风公寓-维修
     */
    @RequiresPermissions("qif:repair:edit")
    @Log(title = "锜风公寓-维修", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QifRepair qifRepair) {
        return toAjax(qifRepairService.updateQifRepair(qifRepair));
    }

    /**
     * 删除锜风公寓-维修
     */
    @RequiresPermissions("qif:repair:remove")
    @Log(title = "锜风公寓-维修", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(qifRepairService.deleteQifRepairByRepairIds(ids));
    }


    @Log(title = "维修状态修改", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:repair:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(QifRepair repair) {
        return toAjax(qifRepairService.changeStatus(repair));
    }

}
